package deque;

/**
 * 每个节点
 */
class Node{
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public class MyQueue {
    public Node head;
    public Node tail;

    /**
     * 插入元素 -- 尾插法
     * @param val
     */
    public void offer(int val) {
        Node node = new Node(val);
        if (head == null) {
            head = node;
            tail = node;
        }else {
            tail.next = node;
            tail = tail.next;
        }
    }

    /**
     * 出队列
     */
    public int poll() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空！");
        }
        int val = head.val;
        head = head.next;
        return val;
    }

    /**
     * 获取队头元素
     */
    public int peek() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空！");
        }
        return head.val;
    }

    // 队列是否为空
    public boolean isEmpty() {
        return head == null;
    }
}
